package org.phoenix.mall.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.phoenix.mall.admin.common.utils.PhoenixResponseJSON;
import org.phoenix.mall.admin.config.annotations.AuthRequired;
import org.phoenix.mall.admin.pojo.Attribute;
import org.phoenix.mall.admin.service.AttributeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(value = "分类参数管理", tags = {"分类参数管理API"})
@RestController
@RequestMapping("attributes")
@AuthRequired
public class AttributeController {
    private AttributeService attributeService;

    @ApiOperation(value = "修改参数项", notes = "修改参数项", httpMethod = "PUT")
    @PutMapping("/{id}")
    public PhoenixResponseJSON edit(@PathVariable Integer id, @RequestBody Map<String, Object> map) {
        Object attrNameObj = map.get("attrName");
        String attrName = attrNameObj == null ? null : attrNameObj.toString();
        Object attrValsObj = map.get("attrVals");
        String attrVals = attrValsObj == null ? null : attrValsObj.toString();
        Attribute attribute = new Attribute();
        attribute.setId(id);
        attribute.setAttrName(attrName);
        attribute.setAttrVals(attrVals);
        int count = attributeService.edit(attribute);
        if(count == 0){
            return PhoenixResponseJSON.errorMsg("修改参数项出错！");
        }
        return PhoenixResponseJSON.ok();
    }

    @ApiOperation(value = "删除参数", notes = "删除参数", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    public PhoenixResponseJSON del(@PathVariable Integer id) {
        Attribute attribute = new Attribute();
        attribute.setId(id);
        int count = attributeService.delete(attribute);
        if(count == 0){
            return PhoenixResponseJSON.errorMsg("修改参数项出错！");
        }
        return PhoenixResponseJSON.ok();
    }


    @Resource
    public void setAttributeService(AttributeService attributeService) {
        this.attributeService = attributeService;
    }
}
